package com.yoooya.base.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yoooya.base.model.vo.BaseResp;

public class ResponseUtils {

	public static final String RESPONSE_SUCCESS_KEY = "success";

	public static final String RESPONSE_FAILURE_KEY = "failure";

	public static final String RESPONSE_TEXT_KEY = "text";

	public static final String RESPONSE_EXTRA_KEY = "extra";

	public static final String PAGINATION_ROOT_PROPERTY_KEY = "rows";

	public static final String DATA_KEY = "data";

	public static final String PAGINATION_TOTAL_PROPERTY_KEY = "totalCount";

	/**
	 * 分页数据
	 * */
	public static <T> Map<String, Object> sendPagination(PageList<T> T) {
		Map<String, Object> map = getInstanceMap();
		if(T!=null&& T.getPaginator()!=null){
			map.put(PAGINATION_TOTAL_PROPERTY_KEY, T.getPaginator().getTotalCount());
		}else{
			map.put(PAGINATION_TOTAL_PROPERTY_KEY,0);
		}
		map.put(PAGINATION_ROOT_PROPERTY_KEY, T);
		return map;
	}

	public static <T> BaseResp paginationResp(PageList<T> T) {
		BaseResp resp = new BaseResp();
		if(T!=null&& T.getPaginator()!=null){
			resp.setCount(T.getPaginator().getTotalCount());
		}else{
			resp.setCount(0);
		}
		resp.setData(T);
		return resp;
	}

	/**
	 * 分页数据
	 * */
	public static <T> Map<String, Object> sendPagination(List<T> T,
			int totalCount) {
		Map<String, Object> map = getInstanceMap();
		map.put(PAGINATION_TOTAL_PROPERTY_KEY, totalCount);
		map.put(PAGINATION_ROOT_PROPERTY_KEY, T);
		return map;
	}

	public static <T> Map<String, Object> sendList(List<T> T) {
		Map<String, Object> map = getInstanceMap();
		map.put(PAGINATION_ROOT_PROPERTY_KEY, T);
		map.put(RESPONSE_SUCCESS_KEY, true);
		return map;
	}

	public static <T, V, K> Map<String, Object> sendList(Map<K, V> T) {
		Map<String, Object> map = getInstanceMap();
		map.put(PAGINATION_ROOT_PROPERTY_KEY, T);
		map.put(RESPONSE_SUCCESS_KEY, true);
		return map;
	}

	public static <T, V, K> Map<String, Object> sendObj(Object obj) {
		Map<String, Object> map = getInstanceMap();
		map.put(DATA_KEY, obj);
		map.put(RESPONSE_SUCCESS_KEY, true);
		return map;
	}

	public static Map<String, Object> sendSuccess(String text, Object... extra) {
		Map<String, Object> map = getInstanceMap();
		map.put(RESPONSE_SUCCESS_KEY, true);
		map.put(RESPONSE_TEXT_KEY, text);
		if (extra.length > 0) {
			map.put(RESPONSE_EXTRA_KEY, extra);
		}
		return map;
	}

	public static Map<String, Object> sendFailure(String text, Object extra) {
		Map<String, Object> map = getInstanceMap();
		map.put(RESPONSE_FAILURE_KEY, true);
		map.put(RESPONSE_TEXT_KEY, text);
		map.put(RESPONSE_EXTRA_KEY, extra);
		return map;
	}

	private static Map<String, Object> getInstanceMap() {
		return new HashMap<String, Object>();
	}

}
